<template>
  <div>
    <!--  顶部面包屑  -->
    <div class="ccui-navigation">
      <!-- 面包屑 -->
      <breadcrumb
        :breadcrumbArr="breadcrumbArr"
        :breadcrumbLastItem="breadcrumbLastItem"
        :state="$route.query.id ? 'edit' : 'create'"
        @handleBack="handleBack"
      />
    </div>
    <div class="ccui-app-container-detail">
      <div class="ccui-app-container-detail-body">
        <el-tabs v-model="activeName" class="el-tabs__customize">
          <el-tab-pane label="基本信息" name="a">
            <basicInfo ref="basicInfo" />
          </el-tab-pane>

          <el-tab-pane label="采购清单" name="b">
            <tableList ref="tableList" />
          </el-tab-pane>

          <el-tab-pane label="附件" name="c">
            <fileInfo
              v-if="isFiles"
              ref="fileInfo"
              v-model="fileList"
              :state="$route.query.id ? 'edit' : 'creat'"
            />
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <!--  按钮  -->
    <el-bottom-guide>
      <ul class="ccui-bottom-button">
        <li>
          <el-button
            type="bottom"
            icon="icon-ccui-save"
            @click="submitHandle('save')"
            >保存并校验
          </el-button>
        </li>
        <li>
          <el-button
            type="bottom"
            icon="icon-ccui-save"
            @click="submitHandle('submit')"
            >保存并提交
          </el-button>
        </li>
        <div class="ccui-bottom-guide-divider" />
        <li>
          <el-button type="bottom" icon="icon-ccui-return" @click="handleBack"
            >返回
          </el-button>
        </li>
      </ul>
    </el-bottom-guide>
  </div>
</template>

<script>
import basicInfo from "@/app/financial/financialOutbound/components/basicInfo";
import tableList from "@/app/financial/financialOutbound/components/tableList";
import fileInfo from "@/basa/components/public/uploadInfo2";
import js from "../js";
import { save, submit, edit, getExwarehouseWriteOff } from "../api";
import publicComponent from "@/basa/components/public";

export default {
  name: "item_new",
  inject: ["reload"],
  components: {
    basicInfo,
    tableList,
    fileInfo,
    breadcrumb: publicComponent.breadcrumb,
  },
  data() {
    return {
      activeName: "a",
      isFiles: false,
      fileList: [],
      breadcrumbArr: js.breadcrumbArr,
      breadcrumbLastItem: "",
    };
  },
  methods: {
    async submitHandle(type) {
      let form = await this.$refs["basicInfo"].handleSubmit();
      if (this.$route.query.id) {
        type == "save" ? this.submitSaveEdit(form) : this.submitCommit(form);
      } else {
        type == "save" ? this.submitSave(form) : this.submitCommit(form);
      }
    },

    // 保存
    submitSave(form) {
      let params = {
        writeOffBO: form,
        files: this.handleFilesParams(),
        isShowLoading: true,
      };
      save(params).then((res) => {
        this.$message({
          type: "success",
          message: res.data.message,
        });
        this.handleBack();
      });
    },

    // 保存提交
    submitCommit(form) {
      let params = {
        writeOffBO: form,
        files: this.handleFilesParams(),
        isShowLoading: true,
      };
      submit(params).then((res) => {
        this.$message({
          type: "success",
          message: res.data.message,
        });
        this.handleBack();
      });
    },

    // 编辑保存
    submitSaveEdit(form) {
      let params = {
        writeOffBO: form,
        files: this.handleFilesParams(),
        isShowLoading: true,
      };
      edit(params).then((res) => {
        this.$message({
          type: "success",
          message: res.data.message,
        });
        this.handleBack();
      });
    },

    // 获取详情
    getDetail() {
      let params = { id: this.$route.query.id, isShowLoading: true };
      getExwarehouseWriteOff(params).then((res) => {
        let result = res.data.data;
        this.$nextTick(() => {
          result.exwarehouseWriteOffBO.exwarehouseCount = (0 - result.exwarehouseWriteOffBO.exwarehouseCount).toFixed(3);
          result.exwarehouseWriteOffBO.exwarehouseMoney = (0 - result.exwarehouseWriteOffBO.exwarehouseMoney).toFixed(2);
          result.exwarehouseWriteOffBO.reportTime =
            result.exwarehouseWriteOffBO.reportTime.substring(
              0,
              result.exwarehouseWriteOffBO.reportTime.indexOf(" ")
            );
          this.$refs["basicInfo"].form = result.exwarehouseWriteOffBO;
          this.breadcrumbLastItem = result.exwarehouseWriteOffBO.writeoffName;
          result.exwarehouseWriteOffBOList.forEach((item) => {
            item.storageCount = (+item.storageCount).toFixed(3);
            item.exwarehouseCount = (0 - item.exwarehouseCount).toFixed(3);
            item.unitPriceWithTax = (+item.unitPriceWithTax).toFixed(2);
            item.exwarehouseMoney = (0 - item.exwarehouseMoney).toFixed(2);
          });
          this.fileList = result.exwarehouseWriteOffFiles;
          this.$refs["tableList"].tableData = result.exwarehouseWriteOffBOList;
        });
        this.isFiles = true;
      });
    },

    // 处理附件入参
    handleFilesParams() {
      let files = [];
      this.fileList.map((e, i) => {
          files.push({
          fileDesc: e.fileDesc,
          sn: i,
          uploadId: e.uploadId,
          uploadTime: e.createTime || e.uploadTime
        });
      });
      return files;
    },

    handleBack() {
      this.$router.push("/inventoryManagement/outBound/all");
      this.reload();
    },
  },
  mounted() {
    this.$route.query.id ? this.getDetail() : (this.isFiles = true);
  },
};
</script>

<style lang="scss" scoped>
/deep/ .file-group .el-group-title .group-content {
  margin-top: 40px;
}

/deep/ .el-group-title.ccui-group-wrapper {
    .extend-btn {
        top: 0;
    }
}

/deep/ .el-tabs__customize {
  padding-bottom: 50px;
}

/deep/ .el-group-title .group-content {
  margin-top: 40px;
}

/deep/ .el-table {
  /deep/ th {
    padding: 0;
    height: 36px;
    line-height: 36px;
  }

  .el-table__body {
    .cell {
      height: 36px;
      line-height: 36px;
    }
  }
}
</style>